package com.remenote.remenoteback.entity.vo;

import com.remenote.remenoteback.entity.doo.CommentDo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: remenote-back
 * @BelongsPackage: com.remenote.remenoteback.entity.vo
 * @Author: 23392
 * @CreateTime: 2024-03-19  16:48
 * @Description: TODO
 * @Version: 1.0
 */
@Getter
@Setter
@Slf4j
@ToString
@Accessors(chain = true)
public class CommentSectionVo {
    private String notebookName ;
    private String noteTitle;
    private List<CommentVo> commentList;

    public static List<CommentSectionVo> fromCommentDoList(List<CommentDo> commentDoList) {
        Map<String, CommentSectionVo> commentSectionVoMap = new HashMap<>();
        for (CommentDo commentDo : commentDoList) {
            String notebookName = commentDo.getNotebookName();
            String noteTitle=commentDo.getNoteTitle();
            String sumKey = notebookName + noteTitle;
            CommentSectionVo commentSectionVo = commentSectionVoMap.get(sumKey);
            if (commentSectionVo == null) {
                commentSectionVo=new CommentSectionVo();
                commentSectionVo.getCommentList().add(CommentVo.fromCommentDo(commentDo));
                commentSectionVo.setNoteTitle(noteTitle);
                commentSectionVo.setNotebookName(notebookName);
                commentSectionVoMap.put(sumKey, commentSectionVo);
            }
        }
        return new ArrayList<>(commentSectionVoMap.values());
    }

    public static CommentSectionVo fromCommentDoList(List<CommentDo> commentDoList, String notebookName, String noteTitle) {
        CommentSectionVo commentSectionVo = new CommentSectionVo();
        commentSectionVo
                .setCommentList(CommentVo.formCommentDoList(commentDoList))
                .setNotebookName(notebookName)
                .setNoteTitle(noteTitle);
        return commentSectionVo;
    }
}
